﻿;*****************************************************************************
;
; Image converter command
; Copyright 2014 Jésahel Benoist
;
; --- License ---
;
; This file is part of TreeProcess
;                                                                   
; TreeProcess is free software: you can redistribute it and/or modify 
; it under the terms of the GNU General Public License as publish
; the Free Software Foundation, either version 3 of the License, 
; (at your option) any later version.                            
;                                                                   
; TreeProcess is distributed in the hope that it will be useful,      
; but WITHOUT ANY WARRANTY; without even the implied warranty of 
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
; GNU General Public License for more details.                   
;                                                                   
; You should have received a copy of the GNU General Public Licence
; along with TreeProcess.  If not, see <http://www.gnu.org/licenses/>.
;
; --- Description ---
;
; This command converts an image to another format
;
; Input parameters 
; SrcImageFilename DstImageFileName Format Flags Depth
;
;  Format = (jpeg, png, bmp, jpeg2000) 
;  Flags  = (compression Or 256 For FloydSteinberg dithering) 
;  Depth  = 1, 2, 4, 8, 24, 32 bits
;
;*****************************************************************************

EnableExplicit

OpenConsole()

UseJPEG2000ImageDecoder()
UseJPEG2000ImageEncoder()
UseJPEGImageDecoder()
UseJPEGImageEncoder()
UsePNGImageDecoder()
UsePNGImageEncoder()
UseTGAImageDecoder()
UseTIFFImageDecoder()

Define SrcFileName.s, DstFileName.s, DstFileFormat.i, Flags, Depth.i

SrcFileName.s = ProgramParameter(0)
If SrcFileName = ""
  PrintN("No image filename given")
  CloseConsole()
  End
EndIf
;If no dest, dest = src
If ProgramParameter(1) = ""
  DstFileName = ProgramParameter(0)
Else  
  DstFileName = ProgramParameter(1)
EndIf

DstFileFormat = #PB_ImagePlugin_PNG
If ProgramParameter(2) <> ""
  Select LCase(ProgramParameter(2))
    Case "bmp", "bitmap"
      DstFileFormat = #PB_ImagePlugin_BMP
    Case "jpeg", "jpg" 
      DstFileFormat = #PB_ImagePlugin_JPEG 
    Case "jpeg2000", "jpg2000"
      DstFileFormat = #PB_ImagePlugin_JPEG2000 
    Case "png", "ping"
      DstFileFormat = #PB_ImagePlugin_PNG
    Default  
  EndSelect   
EndIf
  
If ProgramParameter(3) <> ""
  Flags = Val(ProgramParameter(3))
Else
  Flags = 7
EndIf

If ProgramParameter(4) <> ""
  Depth = Val(ProgramParameter(4))
Else
  Depth = 32
EndIf

If LoadImage(0, SrcFileName)
  If SaveImage(0, DstFileName, DstFileFormat, Flags, Depth)
    PrintN("Converted " + SrcFileName + " to " + DstFileName)
  Else
    PrintN("Can't save image " + SrcFileName)
  EndIf
Else
  PrintN("Can't open image " + SrcFileName)
EndIf

CloseConsole()

End
; IDE Options = PureBasic 5.22 LTS (Windows - x64)
; ExecutableFormat = Console
; CursorPosition = 24
; EnableXP
; UseMainFile = ImageConverter.pb
; Executable = ..\..\Command\ImageConverter.exe
; CurrentDirectory = ..\..\Command\